package com.hundsun.fcp.dao;

import android.annotation.TargetApi;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;

import com.hundsun.fcp.common.logger.Logger;
import com.hundsun.fcp.common.utils.StringUtils;
import com.hundsun.fcp.db.columns.MineColumns;
import com.hundsun.fcp.openapi.trade.bean.ResponseLoginBean;

public class MineDao {
	private static final Logger LOGGER = Logger.getLogger(MineDao.class);
	private Context context;
	private BaseDao baseDao;
	
	public MineDao(Context context){
		baseDao = new BaseDao(context);
		this.context = context;
	}
	
	/**
	 * 根据用户id查询出对应个人信息
	 * @param id
	 * @return
	 */
	public ResponseLoginBean queryMineResult(String clientno){
		if (clientno == null||clientno.equals("")) {
			return null;
		}
		Cursor cursor = null;
		try {
			cursor = baseDao.openR().query(MineColumns.MINE, MineColumns.MINEALL, MineColumns.MINE_CLIENTNO+" =? ", new String[]{clientno}, null, null, null);
			if (cursor.getCount()>0) {
				cursor.moveToFirst();
				ResponseLoginBean mineResult = cursorToLoginResult(cursor);
				return mineResult;
			}else{
				return null;
			}
		}finally{
			baseDao.close(cursor);
		}
		
	}
	
	public void clearMineResult(){
		try {
			baseDao.openW().delete(MineColumns.MINE, null, null) ;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private ResponseLoginBean cursorToLoginResult(Cursor cursor){
		ResponseLoginBean loginResult = new ResponseLoginBean();
		loginResult.setClientno(cursor.getString(cursor.getColumnIndex(MineColumns.MINE_CLIENTNO))) ;
		loginResult.setClientname(cursor.getString(cursor.getColumnIndex(MineColumns.MINE_CLIENTNAME))) ;
		loginResult.setSex(cursor.getString(cursor.getColumnIndex(MineColumns.MINE_SEX))) ;
		loginResult.setBirthday(cursor.getString(cursor.getColumnIndex(MineColumns.MINE_BIRTHDAY))) ;
		loginResult.setShortname(cursor.getString(cursor.getColumnIndex(MineColumns.MINE_SHORTNAME))) ;
		loginResult.setMobile(cursor.getString(cursor.getColumnIndex(MineColumns.MINE_MOBILE))) ;
		 
		return loginResult;
	}
	
	private ContentValues loginResultToContentValues (ResponseLoginBean loginResult){
		ContentValues contentValues = new ContentValues();
		contentValues.put(MineColumns.MINE_CLIENTNO, loginResult.getClientno());
		contentValues.put(MineColumns.MINE_CLIENTNAME, StringUtils.handString(loginResult.getClientname()));
		contentValues.put(MineColumns.MINE_SHORTNAME, StringUtils.handString(loginResult.getShortname()));
		contentValues.put(MineColumns.MINE_SEX, StringUtils.handString(loginResult.getSex()));
		contentValues.put(MineColumns.MINE_BIRTHDAY, StringUtils.handString(loginResult.getBirthday()));
		contentValues.put(MineColumns.MINE_MOBILE, StringUtils.handString(loginResult.getMobile()));
		return contentValues;
	}
	
	
	/**
	 * 首次登陆插入本地表
	 * 后期用于更新本地存在的用户信息
	 * @param loginResult
	 */
	@TargetApi(Build.VERSION_CODES.FROYO)
	public long insertMineResult(ResponseLoginBean loginResult){
		baseDao.openW().delete(MineColumns.MINE, null, null) ;
		return  baseDao.openW().insertWithOnConflict(MineColumns.MINE, null, loginResultToContentValues(loginResult), SQLiteDatabase.CONFLICT_REPLACE);
	}
	
	/**
	 * 更新本地用户的信息
	 * @param id
	 * @param loginResult
	 * @return
	 */
	public long updateMineResult(String clientno,ResponseLoginBean loginResult){
		return baseDao.openW().update(MineColumns.MINE, loginResultToContentValues(loginResult), MineColumns.MINE_CLIENTNO +" =?", new String[]{clientno});
	}
	
	/**
	 * 更新字段
	 * @param clientno
	 * @param columnName
	 * @param columnVaule
	 * @return
	 */
	public long updateMineColumn(String clientno,String columnName,String columnVaule){
		ContentValues contentValues = new ContentValues();
		contentValues.put(columnName, columnVaule);
		return baseDao.openW().update(MineColumns.MINE, contentValues, MineColumns.MINE_CLIENTNO +" =?", new String[]{clientno});
	}
}
